{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "e8d7ba4e-131e-4fab-baeb-bd33f3e8242c",
   "metadata": {},
   "source": [
    "https://python.langchain.com/docs/integrations/llms/tongyi\n",
    "https://dashscope.console.aliyun.com/overview"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "d4691b45-1c9e-4856-884b-0a70d94e6782",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Requirement already satisfied: dashscope in /home/bigmath/miniconda3/lib/python3.11/site-packages (1.15.0)\n",
      "Requirement already satisfied: aiohttp in /home/bigmath/miniconda3/lib/python3.11/site-packages (from dashscope) (3.9.3)\n",
      "Requirement already satisfied: requests in /home/bigmath/miniconda3/lib/python3.11/site-packages (from dashscope) (2.31.0)\n",
      "Requirement already satisfied: aiosignal>=1.1.2 in /home/bigmath/miniconda3/lib/python3.11/site-packages (from aiohttp->dashscope) (1.3.1)\n",
      "Requirement already satisfied: attrs>=17.3.0 in /home/bigmath/miniconda3/lib/python3.11/site-packages (from aiohttp->dashscope) (23.2.0)\n",
      "Requirement already satisfied: frozenlist>=1.1.1 in /home/bigmath/miniconda3/lib/python3.11/site-packages (from aiohttp->dashscope) (1.4.1)\n",
      "Requirement already satisfied: multidict<7.0,>=4.5 in /home/bigmath/miniconda3/lib/python3.11/site-packages (from aiohttp->dashscope) (6.0.5)\n",
      "Requirement already satisfied: yarl<2.0,>=1.0 in /home/bigmath/miniconda3/lib/python3.11/site-packages (from aiohttp->dashscope) (1.9.4)\n",
      "Requirement already satisfied: charset-normalizer<4,>=2 in /home/bigmath/miniconda3/lib/python3.11/site-packages (from requests->dashscope) (3.3.2)\n",
      "Requirement already satisfied: idna<4,>=2.5 in /home/bigmath/miniconda3/lib/python3.11/site-packages (from requests->dashscope) (3.6)\n",
      "Requirement already satisfied: urllib3<3,>=1.21.1 in /home/bigmath/miniconda3/lib/python3.11/site-packages (from requests->dashscope) (1.26.18)\n",
      "Requirement already satisfied: certifi>=2017.4.17 in /home/bigmath/miniconda3/lib/python3.11/site-packages (from requests->dashscope) (2024.2.2)\n"
     ]
    }
   ],
   "source": [
    "# Install the package\n",
    "!pip install --upgrade dashscope"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "08692018-f3b7-4ab6-9d24-23849ecebe20",
   "metadata": {},
   "outputs": [
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      " ········\n"
     ]
    }
   ],
   "source": [
    "# Get a new token: https://help.aliyun.com/document_detail/611472.html?spm=a2c4g.2399481.0.0\n",
    "from getpass import getpass\n",
    "\n",
    "DASHSCOPE_API_KEY = getpass()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "bdcb4ec4-2a56-48a4-a954-96576d799659",
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "\n",
    "os.environ[\"DASHSCOPE_API_KEY\"] = DASHSCOPE_API_KEY"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "9c3f1f7e-6724-4b4f-985f-db4acac3f697",
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain_community.llms import Tongyi\n",
    "\n",
    "tongyi_llm = Tongyi()  # 默认是 qwen-turbo"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "57750388-4a91-46c0-beb8-c075ced1ddd1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'你好！有什么我能为你做的吗？'"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tongyi_llm.invoke(\"你好\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "4ae85795-289c-453b-87d7-c3bf933d57fb",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'你好！有什么我可以帮助你的吗？'"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "qwen_plus = Tongyi(model_name='qwen-plus')\n",
    "qwen_plus.invoke('你好')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "9f4da9f5-cc23-4d55-ad3b-14a0d9507584",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'你好！有什么我能为你做的吗？'"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "qwen_max = Tongyi(model_name='qwen-max')\n",
    "qwen_max.invoke('你好')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "e3c31496-a933-40bc-a911-ffdcf58d5e25",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'你好！有什么我能为你效劳的吗？'"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "qwen_max_1201 = Tongyi(model_name='qwen-max-1201')\n",
    "qwen_max_1201.invoke('你好')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "4ec8c376-3f77-404c-8d37-518ce96cd080",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'你好！有什么我能为你做的吗？'"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "qwen_max_longcontext = Tongyi(model_name='qwen-max-longcontext')\n",
    "qwen_max_longcontext.invoke('你好')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "13441989-d778-448c-984e-a35ba8e87292",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
